﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Preferences
{
    public partial class AddPreference : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["AddPreferrenceLabeltext"] != null && IsPostBack)
            {
                AddPreferrenceLabel.Text = (string)HttpContext.Current.Session["AddPreferrenceLabeltext"];
                //OKButton.Enabled = false;
            }
            else if (IsPostBack)
            {
                AddPreferrenceLabel.Text = "";
            }
            else
            {
                AddPreferrenceLabel.Text = "";
            }
        }
        
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.IsInRole("admin")) Response.Redirect("~/Preferences/ListPreferences.aspx?action=Preferences%20are%20not%20available%20for%20admins");
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton preferencesMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("PreferencesMenuButton");
            preferencesMenuButton.CssClass = "selected";

            if (AddPreferrenceLabel.Text != "") return;
            AddPreferrenceLabel.Text = "Input information for new Preference for " + User.Identity.Name;
        }

        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            LoginView view = (LoginView)this.Master.FindControl("LoginViewForDepartmentSelection");
            DropDownList seldep = (DropDownList)view.FindControl("DepartmentSelection");
            if (seldep != null)
            {
                Department CurrentDepartment = ListOfDepartment.GetDepartment(Name: seldep.SelectedItem.Text);
                if (CurrentDepartment == null) Response.Redirect("~/Preferences/ListPreferences.aspx?action=Invalid%20Department%20Selected");
                if (CurrentDepartment.GetFaculty(Guid.Empty, HttpContext.Current.User.Identity.Name) == null) Response.Redirect("~/Preferences/ListPreferences.aspx?action=Creating%20Preferences%20is%20disabled%20in%20other%20departments");
                if (CurrentDepartment.Schedule != null) Response.Redirect("~/Preferences/ListPreferences.aspx?action=Preferences%20locked%20because%20Schedule%20has%20been%20created");
            }
            else
            {
                Response.Redirect("~/Preferences/ListPreferences.aspx?action=Invalid%20Department%20Selected");
            }
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            if (CourseDropList.SelectedValue == "" || RoomDropList.SelectedValue == "" || TimeSlotDropList.SelectedValue == "")
            {
                String errmsg = "None of the fields may be blank";
                if (AddPreferrenceLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddPreferrenceLabeltext"] = errmsg;
                    Server.Transfer("~/Preferences/AddPreference.aspx", true);
                }
                return;
            }

            LoginView view = (LoginView)this.Master.FindControl("LoginViewForDepartmentSelection");
            DropDownList seldep = (DropDownList)view.FindControl("DepartmentSelection");
            if (seldep != null)
            {
                Department CurrentDepartment = ListOfDepartment.GetDepartment(Name: seldep.SelectedItem.Text);
                if (CurrentDepartment == null) Response.Redirect("~/Preferences/ListPreferences.aspx?action=Invalid%20Department%20Selected");
                Faculty currentfac = CurrentDepartment.GetFaculty(Guid.Empty, HttpContext.Current.User.Identity.Name);
                if(currentfac == null)
                {
                    String errmsg = "Internal error";
                    if (AddPreferrenceLabel.Text != errmsg)
                    {
                        HttpContext.Current.Session["AddPreferrenceLabeltext"] = errmsg;
                        Server.Transfer("~/Preferences/AddPreference.aspx", true);
                    }
                    return;
                }
                int courseid=Convert.ToInt32(CourseDropList.SelectedValue);
                int roomid = Convert.ToInt32(RoomDropList.SelectedValue);
                int timeid = Convert.ToInt32(TimeSlotDropList.SelectedValue);

                if (!currentfac.CreatePreference(courseid, roomid, timeid))
                {
                    String errmsg = "Internal error";
                    if (AddPreferrenceLabel.Text != errmsg)
                    {
                        HttpContext.Current.Session["AddPreferrenceLabeltext"] = errmsg;
                        Server.Transfer("~/Preferences/AddPreference.aspx", true);
                    }
                    return;
                }
                else
                {
                    HttpContext.Current.Session["AddPreferrenceLabeltext"] = "";
                    AddPreferrenceLabel.Text = "Success";
                    Response.Redirect("~/Preferences/ListPreferences.aspx?action=Success");
                }
            }
            else
            {
                Response.Redirect("~/Preferences/ListPreferences.aspx?action=Invalid%20Department%20Selected");
            }
            

        }

        protected void RoomDropList_DataBound(object sender, EventArgs e)
        {
            TimeSlotDropList.DataBind();
        }

    }
}